#!/usr/bin/env python2.7

# bender.py

import os, sys
#import re
#from datetime import datetime, timedelta
#from dateutil.parser import parse
import numpy as np
import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

#from mpl_toolkits.basemap import Basemap
#from matplotlib import mlab
#from netCDF4 import Dataset
from PySide.QtCore import *
from PySide.QtGui import *
from ui_peaker import *
from metlib.kits import Null
from metlib.lidar.peak import *

class Peaker(QWidget, Ui_PeakWidget):
    def __init__(self, parent=None):
        super(Peaker, self).__init__(parent)
        self.setupUi(self)
        self.set_pos_offset_Label()
        self.set_pos_ratio_Label()
        self.set_pos_thres_Label()
        self.set_neg_offset_Label()
        self.set_neg_ratio_Label()
        self.set_neg_thres_Label()

        self.pos_offset_Slider.valueChanged.connect(self.set_pos_offset_Label)
        self.pos_ratio_Slider.valueChanged.connect(self.set_pos_ratio_Label)
        self.pos_thres_Slider.valueChanged.connect(self.set_pos_thres_Label)
        self.neg_offset_Slider.valueChanged.connect(self.set_neg_offset_Label)
        self.neg_ratio_Slider.valueChanged.connect(self.set_neg_ratio_Label)
        self.neg_thres_Slider.valueChanged.connect(self.set_neg_thres_Label)
    
    def calc_marker_size(self, values):
        if np.isscalar(values):
            if values >= 0.0:
                res =  (np.log10(values) + self.pos_offset) * self.pos_ratio
                if res < self.pos_thres: res = 0.0
            else:
                res = (np.log10(-values) + self.neg_offset) * self.neg_ratio
                if res < self.neg_thres: res = 0.0
        else:
            values = np.array(values)
            res = np.zeros_like(values)
            pos_inds = values >= 0.0
            res[pos_inds] = (np.log10(values[pos_inds]) + self.pos_offset) * self.pos_ratio
            res[pos_inds][res[pos_inds] < self.pos_thres] = 0.0
            res[~pos_inds] = (np.log10(values[~pos_inds]) + self.neg_offset) * self.neg_ratio
            res[~pos_inds][res[~pos_inds] < self.neg_thres] = 0.0
        return res

    def peaks_to_scatter(self, dts, heights, peaks_list, pos_color='r', neg_color='b'):
        res_dts = []
        res_heights = []
        res_ss = []
        res_color = []
        for ti in range(len(peaks_list)):
            ps = peaks_list[ti]
            if isinstance(ps, Peak):
                ps = [ps]
            for p in ps:
                ms = self.calc_marker_size(p.depth)
                if ms > 0.0:
                    res_dts.append(dts[ti])
                    res_heights.append(heights[p.center])
                    res_ss.append(ms*ms)
                    res_color.append(pos_color if p.depth>0 else neg_color)
        return res_dts, res_heights, res_ss, res_color

    def set_pos_offset_Label(self, *args):
        self.pos_offset_Label.setNum(self.pos_offset)
    
    def set_pos_ratio_Label(self, *args):
        self.pos_ratio_Label.setNum(self.pos_ratio)
    
    def set_pos_thres_Label(self, *args):
        self.pos_thres_Label.setNum(self.pos_thres)
    
    def set_neg_offset_Label(self, *args):
        self.neg_offset_Label.setNum(self.neg_offset)
    
    def set_neg_ratio_Label(self, *args):
        self.neg_ratio_Label.setNum(self.neg_ratio)
    
    def set_neg_thres_Label(self, *args):
        self.neg_thres_Label.setNum(self.neg_thres)

    @property
    def pos_offset(self):
        return float(self.pos_offset_Slider.value()) / 10.0

    @pos_offset.setter
    def pos_offset(self, value):
        self.pos_offset_Slider.setValue(int(value * 10.0))
    
    @property
    def pos_ratio(self):
        return float(self.pos_ratio_Slider.value()) / 10.0
    
    @pos_ratio.setter
    def pos_ratio(self, value):
        self.pos_ratio_Slider.setValue(int(value * 10.0))
    
    @property
    def pos_thres(self):
        return float(self.pos_thres_Slider.value()) / 100.0
    
    @pos_thres.setter
    def pos_thres(self, value):
        self.pos_thres_Slider.setValue(int(value * 100.0))
    
    @property
    def neg_offset(self):
        return float(self.neg_offset_Slider.value()) / 10.0
    
    @neg_offset.setter
    def neg_offset(self, value):
        self.neg_offset_Slider.setValue(int(value * 10.0))
    
    @property
    def neg_ratio(self):
        return float(self.neg_ratio_Slider.value()) / 10.0
    
    @neg_ratio.setter
    def neg_ratio(self, value):
        self.neg_ratio_Slider.setValue(int(value * 10.0))
    
    @property
    def neg_thres(self):
        return float(self.neg_thres_Slider.value()) / 100.0

    @neg_thres.setter
    def neg_thres(self, value):
        self.neg_thres_Slider.setValue(int(value * 100.0))
    

if __name__ == '__main__':
    pass
